/*
 COPYRIGHT (C) 1984-1989 Apple Computer,Inc.
 All rights reserved
*/

/* Icon Resource Picker */

/* This is the Icon resource picker.  This picker is very similar to all other pickers.
		The general scheme is that a List structure is created whose cells
		contain the ID's of this resource type.  A drawproc is installed in the List
		record which is called to display the resource.  Obviously, graphical resources
		are drawn as is, but descriptions of other types can be displayed also (usually
		in a one-dimensional list). */

#include	<types.h>
#include	<memory.h>
#include	<menus.h>
#include	<resources.h>
#include	<lists.h>

#include	"ResEd.h"

#define listCellSizeH 			0x40
#define listCellSizeV				0x40
#define theScrollBar				15
	
/* Needs to be 2 wide so that I can always tell a 2d list from a normal text list. */
#define minIconsPerRow 			2
#define ICONMinWindowWidth 	(minIconsPerRow * listCellSizeH) + theScrollBar
#define ICONMinWindowHeight listCellSizeV
	
#define ICONResourceSize 		128


/* Used only for editors. */
pascal void EditBirth(Handle thing, ParentHandle dad)
{
	#pragma	unused (thing, dad)
}

/* *********************************************************************************** */

/* Returns the width and the iconsPerRow. */
short GetWidth (short *iconsPerRow)
{
	short width;

	width = PickStdWidth();
	if (width > ICONMinWindowWidth) {
		*iconsPerRow = (width - theScrollBar) / listCellSizeH;
		width = (*iconsPerRow * listCellSizeH) + theScrollBar;
		}
	else {
		width = ICONMinWindowWidth;
		*iconsPerRow = minIconsPerRow;
		}
	return width;
}

/* *********************************************************************************** */

short GetHeight (void)
{
	short height;
	
	height = PickStdRows() * DefaultListCellSize();
	if (height > ICONMinWindowHeight) {
		height = (height / listCellSizeV) * listCellSizeV;
		}
	else {
		height = ICONMinWindowHeight;
		}
	return height;
}

/* *********************************************************************************** */

pascal void PickBirth(ResType t, ParentHandle dad)
{
	PickHandle 	pick;
	PickPtr			p;					/* temp ptr for *pick */
	WindowPtr 	myWindow;
	Str255 			from,
							myTitle;
	Cell				cSize;
	short				iconsPerRow;

	if (t != (ResType)'ICON') {
		return;																/* This only works for ICON resources. 						*/
	}
		
	GetStr(fromStr, miscStrings, &from); 		/* Get ' from ' 																	*/
	strcpy(myTitle, "\pICONs");
	ConcatStr (myTitle, from);
	myWindow = WindSetup(GetWidth (&iconsPerRow), GetHeight(), myTitle, (*dad)->name);
	
	if (myWindow != NULL)	{
		/* Get the memory for my data structure. */
		pick = (PickHandle)NewHandle(sizeof(PickRec));
	
		BubbleUp ((Handle)pick);						/* Move it out of the way. 													*/
		HLock ((Handle)pick);								/* Lock it down so that I can dereference it. 			*/
	
		p = *pick;

		p->father = dad; 							/* Back ptr to dad 																	*/
		memcpy(p->fName, (*dad)->name, (*dad)->name[0] + 1);	/* Add 1 for the length byte */
		p->wind = myWindow; 					/* Directory window 																*/
		p->rebuild = false;
		p->pickID = ResEdID(); 				/* Get my resource id 															*/
		p->rType = 'ICON'; 						/* Resource type that I understand 									*/
		p->rNum = CurResFile(); 			/* Owner res file 																	*/
		p->rSize = ICONResourceSize;
	
		((WindowPeek)myWindow)->windowKind = ResEdID();	/* Get my resource ID 										*/
		((WindowPeek)myWindow)->refCon = (long)pick;	/* Save the data structure for later use.	*/

		/* Create the list. 																																		*/
		cSize.h = listCellSizeH;
		cSize.v = listCellSizeV;
		p->instances = WindList(myWindow, iconsPerRow, cSize, ResEdID());
		
		p->scroll = (*p->instances)->vScroll;
		
		LDoDraw(false, p->instances);	/* Don't draw it as it is built - too slow. 				*/

		/* Fill in the list with the ids of the resources. */
		p->nInsts = BuildType((ResType)'ICON', p->instances);
		
		/* Select the first icon. */
		LSetSelect(true, *(Cell *)0, p->instances);
		LDoDraw(true, p->instances);	/* Next time, draw them. 														*/
	
		/* Need to update here to avoid the following:	The window is activated and the
			user types a character which causes the list to scroll.  The activate event is processed
			first and draws and hilights the first item.	The key down is processed next and scrolls
			the window which has only one item drawn.  An update comes along which includes all but
			the first item (which was updated by LActivate).	The display is left partially updated. */
		BeginUpdate(myWindow);
		LUpdate(myWindow->visRgn, p->instances);
		EndUpdate(myWindow);
	
		HUnlock((Handle)pick);
	}
}

/* Everything except the grow box is taken care of for us by PickEvent. */
pascal void DoEvent(EventRecord *evt, PickHandle pick)
{
	WindowPtr windPtr;
	
	/* Must do our own grow box manipulation so that the min size is set correctly. */
	if ((evt->what == mouseDown) && (FindWindow(evt->where, &windPtr) == inGrow)) {
		GrowMyWindow (ICONMinWindowWidth, ICONMinWindowHeight, windPtr, (*pick)->instances);
		}
	else {
		PickEvent(evt, pick);
		}
}

/* Everything is taken care of for us by PickInfoUp */
pascal void DoInfoUpdate(short oldID, short newID, PickHandle pick)
{
	PickInfoUp(oldID, newID, pick);
}

pascal void DoMenu(short menu, short item, PickHandle pick)
{
	 short 		len, id;
	 Handle		tempH;
	 Cell			theCell;
	 
	/* Since we loaded all of the icons to draw them, we need to release them when we close the window. */
	if ((menu == fileMenu) && (item == closeItem))	{
		BubbleUp((Handle)pick);
		HLock((Handle)pick);
				
		UseResFile((*pick)->rNum);				/* Make sure that we have the correct file. 					*/
		theCell.h = 0;										/* Start at the first cell. 													*/
		theCell.v = 0;
		SetResLoad (false);								/* Don't load them - we're trying to get rid of them! */
		do	{															/* Loop through all of the cells. 										*/
			len = 2;												/* Get the id from the cell. 													*/
			LGetCell((Ptr)(&id), &len, theCell, (*pick)->instances);
			if (len > 0)	{
				tempH = Get1Res ((ResType)'ICON', id);	/* Get the resource if it is loaded. 									*/
				if (tempH != NULL) {						/* If not null then it was loaded. 										*/
					ReleaseResource (tempH);		/* Toss it if it isn't changed. 											*/
				}
			}
		} while (LNextCell (true, true, &theCell, (*pick)->instances));
		SetResLoad (true);								/* Always restore this!																*/
	}
	
	/* Everything else is taken care of for us by PickMenu. 																	*/
	PickMenu(menu, item, pick);
}
